package com.cba.controller.oa;

import cn.hutool.core.bean.BeanUtil;
import com.cba.core.common.R;
import com.cba.core.common.Result;
import com.cba.core.utils.ExcelParseUtil;
import com.cba.service.oa.constart.entity.BO.*;
import com.cba.service.oa.constart.entity.PO.OaWorkTask;
import com.cba.service.work.entity.PO.WorkTask;
import com.cba.service.work.service.IWorkTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wyq
 */
@RestController
@RequestMapping("/oaTask")
@Api(tags = "oa任务")
public class OaTaskController {

    @Autowired
    private IWorkTaskService workTaskService;
//    @Autowired
//    private IOaTaskService oaTaskService;

    @PostMapping("/saveWorkTask")
    public Result saveWorkTask(@RequestBody OaWorkTask oaWorkTask){
//        return oaTaskService.saveWorkTask(oaWorkTask);
        WorkTask workTask = BeanUtil.toBean(oaWorkTask,WorkTask.class);
        workTaskService.saveWorkTask(workTask);
        return R.ok();
    }

    @PostMapping("/setWorkTaskStatus")
    @ApiOperation("设置项目任务状态")
    public Result setWorkTaskStatus(@RequestBody WorkTaskStatusBO workTaskStatusBO){
//        return oaTaskService.setWorkTaskStatus(workTaskStatusBO);
        com.cba.service.work.entity.BO.WorkTaskStatusBO statusBO = BeanUtil.toBean(workTaskStatusBO,com.cba.service.work.entity.BO.WorkTaskStatusBO.class);
        workTaskService.setWorkTaskStatus(statusBO);
        return R.ok();
    }

    @PostMapping("/setWorkTaskTitle")
    @ApiOperation("设置项目任务标题")
    public Result setWorkTaskTitle(@RequestBody WorkTaskNameBO workTaskNameBO){
//        return oaTaskService.setWorkTaskTitle(workTaskNameBO);
        com.cba.service.work.entity.BO.WorkTaskNameBO taskNameBO = BeanUtil.toBean(workTaskNameBO,com.cba.service.work.entity.BO.WorkTaskNameBO.class);
        workTaskService.setWorkTaskTitle(taskNameBO);
        return R.ok();
    }

    @PostMapping("/setWorkTaskDescription")
    @ApiOperation("设置项目任务描述")
    public Result setWorkTaskDescription(@RequestBody WorkTaskDescriptionBO workTaskDescriptionBO){
//        return oaTaskService.setWorkTaskDescription(workTaskDescriptionBO);
        com.cba.service.work.entity.BO.WorkTaskDescriptionBO taskDescriptionBO = BeanUtil.toBean(workTaskDescriptionBO,com.cba.service.work.entity.BO.WorkTaskDescriptionBO.class);
        workTaskService.setWorkTaskDescription(taskDescriptionBO);
        return R.ok();

    }

    @PostMapping("/setWorkTaskMainUser")
    @ApiOperation("设置项目任务负责人")
    public Result setWorkTaskMainUser(@RequestBody WorkTaskUserBO workTaskUserBO){
//        return oaTaskService.setWorkTaskMainUser(workTaskUserBO);
        com.cba.service.work.entity.BO.WorkTaskUserBO taskDescriptionBO = BeanUtil.toBean(workTaskUserBO,com.cba.service.work.entity.BO.WorkTaskUserBO.class);
        workTaskService.setWorkTaskMainUser(taskDescriptionBO);
        return R.ok();
    }

    @PostMapping("/setWorkTaskOwnerUser")
    @ApiOperation("新建项目任务")
    public Result setWorkTaskOwnerUser(@RequestBody WorkTaskOwnerUserBO workTaskOwnerUserBO){
//        return oaTaskService.setWorkTaskOwnerUser(workTaskOwnerUserBO);
        com.cba.service.work.entity.BO.WorkTaskOwnerUserBO taskDescriptionBO = BeanUtil.toBean(workTaskOwnerUserBO,com.cba.service.work.entity.BO.WorkTaskOwnerUserBO.class);
        workTaskService.setWorkTaskOwnerUser(taskDescriptionBO);
        return R.ok();
    }

    @PostMapping("/setWorkTaskTime")
    @ApiOperation("设置项目任务开始结束时间")
    public Result setWorkTaskTime(@RequestBody OaWorkTask oaWorkTask){
//        return oaTaskService.setWorkTaskTime(oaWorkTask);
        com.cba.service.work.entity.PO.WorkTask taskDescriptionBO = BeanUtil.toBean(oaWorkTask,com.cba.service.work.entity.PO.WorkTask.class);
        workTaskService.setWorkTaskTime(taskDescriptionBO);
        return R.ok();
    }

    @PostMapping("/setWorkTaskLabel")
    @ApiOperation("设置项目任务标签")
    public Result setWorkTaskLabel(@RequestBody WorkTaskLabelsBO workTaskLabelsBO){
//        return oaTaskService.setWorkTaskLabel(workTaskLabelsBO);
        com.cba.service.work.entity.BO.WorkTaskLabelsBO taskDescriptionBO = BeanUtil.toBean(workTaskLabelsBO,com.cba.service.work.entity.BO.WorkTaskLabelsBO.class);
        workTaskService.setWorkTaskLabel(taskDescriptionBO);
        return R.ok();
    }

    @PostMapping("/setWorkTaskPriority")
    @ApiOperation("设置项目任务优先级")
    public Result setWorkTaskPriority(@RequestBody WorkTaskPriorityBO workTaskPriorityBO){
//        return oaTaskService.setWorkTaskPriority(workTaskPriorityBO);
        com.cba.service.work.entity.BO.WorkTaskPriorityBO taskDescriptionBO = BeanUtil.toBean(workTaskPriorityBO,com.cba.service.work.entity.BO.WorkTaskPriorityBO.class);
        workTaskService.setWorkTaskPriority(taskDescriptionBO);
        return R.ok();
    }

    @PostMapping("/addWorkChildTask")
    @ApiOperation("新建项目子任务")
    public Result addWorkChildTask(@RequestBody OaWorkTask oaWorkTask){
//        return oaTaskService.addWorkChildTask(oaWorkTask);
        WorkTask workTask = BeanUtil.toBean(oaWorkTask,WorkTask.class);
        WorkTask task = workTaskService.addWorkChildTask(workTask);
        return R.ok(task);
    }

    @PostMapping("/updateWorkChildTask")
    @ApiOperation("编辑项目子任务")
    public Result updateWorkChildTask(@RequestBody OaWorkTask oaWorkTask){
//        return oaTaskService.updateWorkChildTask(oaWorkTask);
        WorkTask workTask = BeanUtil.toBean(oaWorkTask,WorkTask.class);
        workTaskService.updateWorkChildTask(workTask);
        return R.ok();
    }

    @PostMapping("/setWorkChildTaskStatus")
    @ApiOperation("设置项目子任务状态")
    public Result setWorkChildTaskStatus(@RequestBody WorkTaskStatusBO workTaskStatusBO){
//        return oaTaskService.setWorkChildTaskStatus(workTaskStatusBO);
        com.cba.service.work.entity.BO.WorkTaskStatusBO taskDescriptionBO = BeanUtil.toBean(workTaskStatusBO,com.cba.service.work.entity.BO.WorkTaskStatusBO.class);
        workTaskService.setWorkChildTaskStatus(taskDescriptionBO);
        return R.ok();
    }

    @PostMapping("/deleteWorkTaskOwnerUser")
    @ApiOperation("删除项目子任务负责人")
    public Result deleteWorkTaskOwnerUser(@RequestBody WorkTaskUserBO workTaskUserBO){
//        return oaTaskService.deleteWorkTaskOwnerUser(workTaskUserBO);
        com.cba.service.work.entity.BO.WorkTaskUserBO taskDescriptionBO = BeanUtil.toBean(workTaskUserBO,com.cba.service.work.entity.BO.WorkTaskUserBO.class);
        workTaskService.deleteWorkTaskOwnerUser(taskDescriptionBO);
        return R.ok();
    }

    @PostMapping("/deleteWorkTaskLabel")
    @ApiOperation("删除项目任务标签")
    public Result deleteWorkTaskLabel(@RequestBody WorkTaskLabelBO workTaskLabelBO){
//        return oaTaskService.deleteWorkTaskLabel(workTaskLabelBO);
        com.cba.service.work.entity.BO.WorkTaskLabelBO taskDescriptionBO = BeanUtil.toBean(workTaskLabelBO,com.cba.service.work.entity.BO.WorkTaskLabelBO.class);
        workTaskService.deleteWorkTaskLabel(taskDescriptionBO);
        return R.ok();
    }

    @PostMapping("/deleteWorkChildTask/{taskId}")
    @ApiOperation("删除子任务")
    public Result deleteWorkChildTask(@PathVariable Integer taskId){
//        return oaTaskService.deleteWorkChildTask(taskId);
        return R.ok(workTaskService.queryTaskInfo(taskId));
    }

    @PostMapping("/queryTaskList")
    @ApiOperation("查看oa任务列表")
    public Result<com.cba.service.work.entity.VO.OaTaskListVO> queryTaskList(@RequestBody OaTaskListBO oaTaskListBO){
//        Result<OaTaskListVO> listVO = oaTaskService.queryTaskList(oaTaskListBO);
//        return listVO;
        com.cba.service.work.entity.BO.OaTaskListBO taskDescriptionBO = BeanUtil.toBean(oaTaskListBO,com.cba.service.work.entity.BO.OaTaskListBO.class);
        com.cba.service.work.entity.VO.OaTaskListVO listVO =  workTaskService.queryTaskList(taskDescriptionBO);
        return R.ok(listVO);
    }

    @PostMapping("/oaTaskExport")
    @ApiOperation("任务导出")
    public void oaTaskExport(@RequestBody OaTaskListBO oaTaskListBO,HttpServletResponse response) {
        oaTaskListBO.setIsExport(true);
//        OaTaskListVO data = oaTaskService.queryTaskList(oaTaskListBO).getData();
        com.cba.service.work.entity.BO.OaTaskListBO taskDescriptionBO = BeanUtil.toBean(oaTaskListBO,com.cba.service.work.entity.BO.OaTaskListBO.class);
        com.cba.service.work.entity.VO.OaTaskListVO data =  workTaskService.queryTaskList(taskDescriptionBO);
        List<Map<String, Object>> list = data.getExportList();
        List<ExcelParseUtil.ExcelDataEntity> dataList = new ArrayList<>();
        dataList.add(ExcelParseUtil.toEntity("name", "任务名称"));
        dataList.add(ExcelParseUtil.toEntity("description", "任务描述"));
        dataList.add(ExcelParseUtil.toEntity("mainUserName", "负责人"));
        dataList.add(ExcelParseUtil.toEntity("startTime", "开始时间"));
        dataList.add(ExcelParseUtil.toEntity("stopTime", "结束时间"));
        dataList.add(ExcelParseUtil.toEntity("labelName", "标签"));
        dataList.add(ExcelParseUtil.toEntity("ownerUserName", "参与人"));
        dataList.add(ExcelParseUtil.toEntity("priority", "优先级"));
        dataList.add(ExcelParseUtil.toEntity("createUserName", "创建人"));
        dataList.add(ExcelParseUtil.toEntity("createTime", "创建时间"));
        dataList.add(ExcelParseUtil.toEntity("relateCrmWork", "关联业务"));
        ExcelParseUtil.exportExcel(list, new ExcelParseUtil.ExcelParseService() {
            @Override
            public void castData(Map<String, Object> record, Map<String, Integer> headMap) {

            }
            @Override
            public String getExcelName() {
                return "任务";
            }
        }, dataList);
    }

}
